package com.clkj.cldisabledmanclient.mvp.main.index;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.adapter.GvAdapterForApply;
import com.clkj.cldisabledmanclient.adapter.ListAdapterForServiceIntro;
import com.clkj.cldisabledmanclient.adapter.rv.RvAdapterForHelpInfo;
import com.clkj.cldisabledmanclient.adapter.rv.RvAdapterForSelfApply;
import com.clkj.cldisabledmanclient.base.BaseFragment;
import com.clkj.cldisabledmanclient.bean.custombean.IndexApplyItem;
import com.clkj.cldisabledmanclient.bean.custombean.IndexHelpInfoBean;
import com.clkj.cldisabledmanclient.bean.custombean.IndexIntroBean;
import com.clkj.cldisabledmanclient.mvp.serviceintroduction.ServiceIntroductionActivity;
import com.clkj.cldisabledmanclient.util.LogUtil;
import com.clkj.cldisabledmanclient.util.Res;
import com.clkj.cldisabledmanclient.widget.ListViewForScrollView;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by clboys on 2019/5/15.
 */

public class FragmentIndex extends BaseFragment {

    public String mArgument;

    public static final String ARGUMENT = "argument";

    //默认的是isFirstVisibles true ,刚进来的时候分两种情况
    //1.如果切过来的时候 setVisibleHint在OnCreate之前调用，那isFirstVisible为true，这个时候不进行任何操作；
    //等方法执行到onActivityCreated的时候，在执行一次setUserVisibleHint 这个时候把默认的是isFirstVisibles 设为false
    //然后这个时候调用setUserVISIBLEhINT为true  去获取数据
    //2.如果是后面再次切过来，那isFirstVisible已经为false，那就可以直接通过setUserVisibleHint获取数据了

    //代表这个界面是不是第一次被使用
    boolean isFirstUsed = true;
    boolean isGetData = false;

    // private GridViewForScrollView gvSelfApply;
    private ListViewForScrollView lvServiceIntro;
    // private GridViewForScrollView gvHelpInfo;
    private RecyclerView rvSelfApply;
    private RecyclerView rvHelpInfo;

    GvAdapterForApply mAdapterForApply;
    List<IndexApplyItem> mApplyItemList = new ArrayList<>();
    RvAdapterForSelfApply mAdapterForSelfApply;


    ListAdapterForServiceIntro mAdapterForServiceIntro;
    List<IndexIntroBean> mIntroBeanList = new ArrayList<>();


    RvAdapterForHelpInfo mAdapterForHelpInfo;
    List<IndexHelpInfoBean> mIndexHelpInfoBeanList = new ArrayList<>();
    private Banner mMBanner;


    public static FragmentIndex getInstance(String argument) {
        Bundle bundle = new Bundle();
        bundle.putString(ARGUMENT, argument);

        FragmentIndex fragment = new FragmentIndex();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment_index, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        assignView(view);
        initTitle(view, null, null, "首页", false, null);
        initView();
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //fragment 可见的情况下 进行加载数据。不可见则不加载
        if (getUserVisibleHint() == true) {
            if (isFirstUsed == true) {
                isFirstUsed = false;
                setUserVisibleHint(true);
            }
            //不可见，说明fragment已经初始话完成,就直接把isFirstVisible改为false 再次切过来的时候就直接可以加载数据了
        } else {
            isFirstUsed = false;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        // LogUtil.e("CartVisible");
        if (getUserVisibleHint()) {
            LogUtil.e("MyShuoShuoVisible");
            //如果不是firstvisible才去进行下面的操作  因为第一次很可能出现各种问题
            if (isFirstUsed == false) {
                if (isGetData == false) {

                    //获取数据

                }

            }
        } else {
            LogUtil.e("faXianFragmentVisible");
        }
    }


    @Override
    public void initData() {


        IndexApplyItem applyItemForDisableChildTrain = new IndexApplyItem(Res.getString(R.string.str_apply_of_disbaled_child_train), R.mipmap.pic_apply_of_disabled_child_train);
        IndexApplyItem applyItemForEyeRecoverOperation = new IndexApplyItem(Res.getString(R.string.str_apply_of_eye_recover_operation), R.mipmap.pic_apply_of_eye_recover_operation);
        IndexApplyItem applyItemForMentalDiseaseTakeMedicine = new IndexApplyItem(Res.getString(R.string.str_apply_of_mental_disease_take_medicine), R.mipmap.pic_apply_of_mental_disease_take_medicine);
        IndexApplyItem applyItemForAssistDeviceAdaptation = new IndexApplyItem(Res.getString(R.string.str_apply_of_assist_device_adaptation), R.mipmap.pic_apply_of_assist_device_adaptation);


        mApplyItemList.add(applyItemForDisableChildTrain);
        mApplyItemList.add(applyItemForEyeRecoverOperation);
        mApplyItemList.add(applyItemForMentalDiseaseTakeMedicine);
        mApplyItemList.add(applyItemForAssistDeviceAdaptation);

        IndexIntroBean introItemOfDisabledChildTrain = new IndexIntroBean(Res.getString(R.string.str_intro_of_disabled_child_train_apply),"儿童康复", R.mipmap.pic_intro_of_disabled_child_train_apply);
        IndexIntroBean introItemOfEyeRecoverOperation = new IndexIntroBean(Res.getString(R.string.str_intro_of_eye_recover_operation_apply), "白内障手术",R.mipmap.pic_intro_of_eye_recover_operation_apply);
        IndexIntroBean introItemOfMentalDiseaseTakeMedicine = new IndexIntroBean(Res.getString(R.string.str_intro_of_mental_disease_take_medicine_apply),"精神病服药", R.mipmap.pic_intro_of_mental_disease_take_medicine_apply);
        IndexIntroBean introItemOfAssistDeviceAdaptation = new IndexIntroBean(Res.getString(R.string.str_intro_of_assist_device_adaptation_apply), "辅具申请",R.mipmap.pic_intro_of_assist_device_adaptation_apply);


        mIntroBeanList.add(introItemOfDisabledChildTrain);
        mIntroBeanList.add(introItemOfEyeRecoverOperation);
        mIntroBeanList.add(introItemOfMentalDiseaseTakeMedicine);
        mIntroBeanList.add(introItemOfAssistDeviceAdaptation);


        IndexHelpInfoBean helpInfoForRecover = new IndexHelpInfoBean(Res.getString(R.string.str_help_info_of_recover), R.mipmap.pic_help_info_of_recover);
        IndexHelpInfoBean helpInfoForAssistDevice = new IndexHelpInfoBean(Res.getString(R.string.str_help_info_of_assist_device), R.mipmap.pic_help_info_of_assist_device);
        IndexHelpInfoBean helpInfoForRecoverOrg = new IndexHelpInfoBean(Res.getString(R.string.str_help_info_of_recover_org), R.mipmap.pic_help_info_of_recover_org);
        IndexHelpInfoBean helpInfoForSuggestion = new IndexHelpInfoBean(Res.getString(R.string.str_help_info_of_suggestion), R.mipmap.pic_help_info_of_suggest);

        mIndexHelpInfoBeanList.add(helpInfoForRecover);
        mIndexHelpInfoBeanList.add(helpInfoForAssistDevice);
        mIndexHelpInfoBeanList.add(helpInfoForRecoverOrg);
        mIndexHelpInfoBeanList.add(helpInfoForSuggestion);

    }

    @Override
    public void assignView(View view) {

        mMBanner = view.findViewById(R.id.mBanner);
        //gvSelfApply = (GridViewForScrollView) view.findViewById(R.id.gv_self_apply);
        lvServiceIntro = (ListViewForScrollView) view.findViewById(R.id.lv_service_intro);
        //gvHelpInfo = (GridViewForScrollView) view.findViewById(R.id.gv_help_info);

        rvSelfApply = (RecyclerView) view.findViewById(R.id.rv_self_apply);
        rvHelpInfo = (RecyclerView) view.findViewById(R.id.rv_help_info);

    }

    @Override
    public void initView() {

       /* mAdapterForApply = new GvAdapterForApply(mApplyItemList, getActivity());
        gvSelfApply.setAdapter(mAdapterForApply);*/


        mAdapterForServiceIntro = new ListAdapterForServiceIntro(mIntroBeanList, getActivity());
        lvServiceIntro.setAdapter(mAdapterForServiceIntro);


        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        rvSelfApply.setLayoutManager(gridLayoutManager);
        mAdapterForSelfApply = new RvAdapterForSelfApply(mApplyItemList, getActivity());
        //mIvAttachmentAdapter.setAttachmentDelteClickListener(new AttachmentDelteClickListenerImpl());
        rvSelfApply.setAdapter(mAdapterForSelfApply);


        GridLayoutManager gridLayoutManagerForHelpInfo = new GridLayoutManager(getActivity(), 2);
        rvHelpInfo.setLayoutManager(gridLayoutManagerForHelpInfo);
        mAdapterForHelpInfo = new RvAdapterForHelpInfo(mIndexHelpInfoBeanList, getActivity());
        //mIvAttachmentAdapter.setAttachmentDelteClickListener(new AttachmentDelteClickListenerImpl());
        rvHelpInfo.setAdapter(mAdapterForHelpInfo);


        //给banner设置宽高比
       /* ViewGroup.LayoutParams lp;
        lp = mMBanner.getLayoutParams();
        int targetHeight = ScreenUtils.getScreenHeight(getContext());
        int targetWidth = ScreenUtils.getScreenWidth(getContext());
        lp.width = targetWidth;
        lp.height = (int) (targetWidth / 2.08);
        mMBanner.setLayoutParams(lp);

        List<Integer> images = new ArrayList<>();

        images.add(R.mipmap.bg_top);
        mMBanner.setImages(images)
                .setImageLoader(new GlideBannerImageLoader())
                .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                .start();*/
        //服务说明
        lvServiceIntro.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent toIntroduction=new Intent(getActivity(), ServiceIntroductionActivity.class);
                toIntroduction.putExtra("toptitle",mIntroBeanList.get(i).getIntroName());
                toIntroduction.putExtra("type",mIntroBeanList.get(i).getType());
                startActivity(toIntroduction);
            }
        });

    }
}
